package com.example.storagemodule

import android.app.ProgressDialog
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import androidx.annotation.RequiresApi
import kotlinx.android.synthetic.main.activity_net_work.*
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.net.URL
import javax.net.ssl.HttpsURLConnection
import kotlin.concurrent.thread

class NetWorkActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_net_work)
    }

    /**
     * 1.显示ProgressDialog
     * 2.启动分线程
     * 3.在分线程发送请求，得到响应数据
     */
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    fun testConnectionGet(view: View) {

        val progressDialog= ProgressDialog.show(this,null,"处理请求中...")


        val response = StringBuilder()
        thread{
            val URL = URL(et_network_url.text.toString())
            val connection=URL.openConnection() as HttpsURLConnection
            connection.connectTimeout=8000
            connection.readTimeout=8000
            val inputstream = connection.inputStream
            val bufferedReader = BufferedReader(InputStreamReader(inputstream))
            var len = 0;
            bufferedReader.use {
                bufferedReader.forEachLine {
                    response.append(it)
                }
            }
            runOnUiThread {
                et_network_result.setText(response.toString())
                progressDialog.dismiss()
            }
        }
    }
    fun testConnectionPost(view: View) {}
    fun testClientGet(view: View) {}
    fun testClientPost(view: View) {}
    fun testVolleyGet(view: View) {}
    fun testVolleyPost(view: View) {}
}